#ifndef __CLIENT_REQUEST_CHECK_included
#define __CLIENT_REQUEST_CHECK_included

#include <iostream>
#include <string>
#include "ClientRequest.h"
#include "../client-common/VersionTag.h"

using namespace Engine;
using namespace Client::Common;

class CRequestCheck : public CClientRequest {

public :
	CRequestCheck(string& target) : CClientRequest(string(COMMAND_CHECK)) 
	{
		AddParameter(string("target"), target);
	}

	virtual void HandleResponse(boost::shared_ptr<CResponse> response) {

		string s;
		response->GetContentAsString(s);
		DebugStream << "Check Response - " << s << '\n';

		TableRef tvt = CVersionTag::GetSerializedTable(s);
		if(tvt->GetCount() == 0) return;

		boost::shared_array<RecordRef> recs = tvt->NextRecords(tvt->GetCount());
		int count = tvt->GetCurrentRecordsCount();

		for(int i = 0; i < count; i++) {
			VersionTagRef vt = boost::dynamic_pointer_cast<CVersionTag, CRecord>(recs[i]);
			VersionTagRef local_vt = CVersionTag::QueryDeviceTag(vt->mDevice, vt->mTagName);
			if(!local_vt || local_vt->mHeadVersion < vt->mHeadVersion) {
				TaskRef t = CTaskFactory::Create(vt);
				mConnection->AddTask(t);
			}
		}
	}

};
#endif //__ENGINE_REQUEST_CHECK_included